package com.wwwc.index.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.*;
import java.net.URL;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.http.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wwwc.util.web.*;

public class SendMail {

    private StringBuffer result = null;
    private String username = null;
    private String password = null;

    public boolean send(String from, String euser, String epass, String to, String subject, String content) {
	
	if(from == null || euser == null || epass == null || to == null || subject == null) {
	    return false;
	}

	username = euser;
	password = epass;
	result = new StringBuffer();

	try {
	    MyUserInputCheck ck = new MyUserInputCheck();
	    if(!ck.checkEmail(to)) {
		result.append(ck.getMessage());
		return false;
	    }

	    Properties properties = System.getProperties();//new Properties(); 
	    Enumeration enum = properties.propertyNames();
	    String temp = null;
	    while(enum.hasMoreElements()) {
		temp = ""+enum.nextElement();
		System.out.println(temp+"="+properties.getProperty(temp));
	    }
	    properties.put("mail.smtp.auth", "true");

	    Session msession = Session.getInstance(properties, new SmtpAuthenticator()); 
	    if(msession == null) {
		result.append("E-Mail Server Error");
		System.out.println("SendMail:Error:[No Mail Session]");
		return false;
	    }

	    Message message  = new MimeMessage(msession);

	    if(message == null) {
		result.append("E-Mail Server Error");
		System.out.println("SendMail:Error:[No Mail Message]");
		return false;
	    }

	    message.setFrom(new InternetAddress(from));
	    message.setRecipient(Message.RecipientType.TO, new InternetAddress(to, to));
	    InternetAddress dests[] = new InternetAddress[] { new InternetAddress(from) };
	    message.setReplyTo(dests);
	    message.setSentDate(new Date());
	    message.setSubject(subject);
	    message.setContent(content, "text/plain");

	    Transport.send(message);

	    username = null;
	    password = null;

	}
	catch (Exception e) {
	    result.append("E-mail Server:Error");
	    System.out.println("SendMail:Error:"+result);
	    return false;
	}
	catch (Throwable e) {
	    result.append("E-mail Server:Error");
	    System.out.println("SendMail:Error:"+result);
	    return false;
	}
	return true;
    }

    public StringBuffer getError() {
	return result;
    }

    private class SmtpAuthenticator extends Authenticator {
	protected PasswordAuthentication getPasswordAuthentication() {
	    return new PasswordAuthentication (username, password); 
	} 
    } 
}
